/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlunit.code;

/**
 *
 * @author TiTa
 */
public class vision_v1 extends Thread{
    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author TiTa
 */

      parse_google_htmlUnit pgh=new parse_google_htmlUnit();
     private boolean paused;
    private boolean running;
     private queue_url qu;
    public vision_v1() {
        paused = false;
        running = true;
    }
   
    public vision_v1(queue_url ht_queue_url)
    {
        qu=ht_queue_url;
    }
     public void Stop() {
        running = false;
        if (paused)
            interrupt();
        try {
            join();
        } catch (InterruptedException ex) {
        }
    }
       public void Pause() {
        paused = true;
    }

    public void Resume() {      
        interrupt();
        paused = false;
    }
    
    
    public void crawler_google(String url_one,int soluonglink)
    {
      
       
  //      fetch_footer_google ffg=new fetch_footer_google();
        
//     ket_noi_url knu=new ket_noi_url();
     
     
 //       HtmlPage page= knu.ketnoi(url_one);
       // pgh.parse_page_google(url_one);
        for(int j=0;j<soluonglink;j+=10){
           // String foot_google="https://www.google.com.vn/search?q=site:facebook.com+cafe+tphcm&prmd=ivnsm&ei=y4nBUq-QNcW6lAWNsIGYAw&start="+j+"&sa=N";
             String foot_google=url_one+"&prmd=ivnsm&ei=y4nBUq-QNcW6lAWNsIGYAw&start="+j+"&sa=N";
           // String foot_google=url_one+j;        
            pgh.parse_page_google(foot_google);
            
               
          
        }
         // final HtmlTable link_footer_trang10= (HtmlTable) page.getByXPath("//table[@id='nav']").get(0);
     
        // String link_foot10=link_footer_trang10.getElementsByTagName("a").get(11).getTextContent();//so trang
   // String link10=link_footer_trang10.getElementsByTagName("a").get(11).getAttribute("href");//
    // System.out.println(link_foot10+"\n"+link10+"\n");
     
     
     
     
     
    }
    //phuong thuc lay thong tin face
    public void crawler_facebook(String face) throws Exception{
        String face1=xu_ly_chuoi(face);
        parse_not_page_facebook pnpf=new parse_not_page_facebook();
        parse_page_facebook ppf=new parse_page_facebook();
        if(face1.equals("pages")){
            ppf.parse_facebook_is_page_fbLongBlurb(face);
            // pnpf.parse_Profile_facebook(face);
        }else{
            //pnpf.parse_Profile_facebook(face);
            ppf.parse_facebook_is_page_fbLongBlurb(face);
        }
    }
    
 // su ly chuoi   
     private String xu_ly_chuoi(String s){
         
         s = s.substring(25,30);
           return s;
     }
     //
     
     
     @Override
     public void run(){while (running) {//running =true
            while (!paused){//pause=false
                try {
                    Thread.sleep(500);
                    System.out.println("running");
                    String url_one_main="https://www.google.com.vn/search?q=site:facebook.com+cafe+tphcm";
                    int a=10;
                    crawler_google(url_one_main,a);
                  //  thread_queue po=new thread_queue(qu);
                   // po.start();
                   //  Pause();
                  //   Stop();
                } catch (InterruptedException e) {
                    paused = true;
                }
            }
     
     }
     }
     
     
     
     
    public static void main(String[] args) throws InterruptedException{
         //vision_1 vs=new vision_1();
        queue_url qu=new queue_url();
        
        
        
    
      
        thread_queue po=new thread_queue(qu);
        parse_google_htmlUnit ps=new parse_google_htmlUnit(qu);
        kiemtra_net ktn=new kiemtra_net();
   
        if(ktn.kiemtra()==true){
            
             ps.start();
             po.start();
                  
                  
                 String y= po.getName();
                  System.out.println(y);//la cai goi lay
                   String y1= ps.getName();
                  System.out.println(y1);
    
         
       
                /*lay footer facebook
        String url_one_main1="https://www.facebook.com/pages/V%E1%BB%ABng-%C6%A1i-m%E1%BB%9F-ra-Cafe/141160225904005";
         vs.crawler_facebook(url_one_main1);
          */
         
        
    }
        try{
            ps.join();
      
            po.join();
        }catch(InterruptedException ex){
        
            System.out.println("Interrupted"+ex.getMessage());
        }
    }


}
